package com.example.demo.mapper;

import com.example.demo.model.entity.audit.AuditRecord;
import com.example.demo.model.enums.AuditResult;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AuditRecordMapper {
    // 根据成果 ID 查询审核记录
    @Select("SELECT * FROM achievement_audit WHERE achievement_id = #{achievementId}")
    List<AuditRecord> findByAchievementId(Long achievementId);

    // 根据审核用户 ID 分页查询审核记录
    @Select("SELECT * FROM achievement_audit WHERE audit_user_id = #{auditUserId} LIMIT #{offset}, #{limit}")
    List<AuditRecord> findByAuditUserIdPage(Long auditUserId, int offset, int limit);

    // 根据审核用户 ID 查询所有审核记录
    @Select("SELECT * FROM achievement_audit WHERE audit_user_id = #{auditUserId}")
    List<AuditRecord> findByAuditUserId(Long auditUserId);

    // 根据审核结果查询审核记录
    @Select("SELECT * FROM achievement_audit WHERE audit_result = #{result}")
    List<AuditRecord> findByAuditResult(AuditResult result);

    // 根据成果 ID 和审核结果分页查询审核记录
    @Select("SELECT * FROM achievement_audit WHERE achievement_id = #{achievementId} AND audit_result = #{result} LIMIT #{offset}, #{limit}")
    List<AuditRecord> findByAchievementIdAndAuditResultPage(Long achievementId, AuditResult result, int offset, int limit);

    // 插入审核记录
    @Insert("INSERT INTO achievement_audit (achievement_id, audit_user_id, audit_result, audit_comment) " +
            "VALUES (#{achievementId}, #{auditUserId}, #{auditResult}, #{auditComment})")
    @Options(useGeneratedKeys = true, keyProperty = "auditId", keyColumn = "audit_id")
    int insert(AuditRecord record);

    // 根据成果ID和审核结果查询所有记录（非分页）
    @Select("SELECT * FROM achievement_audit WHERE achievement_id = #{achievementId} AND audit_result = #{result}")
    List<AuditRecord> findByAchievementIdAndAuditResult(Long achievementId, AuditResult result);

    // 新增：根据审核用户ID和审核结果查询所有记录
    @Select("SELECT * FROM achievement_audit WHERE audit_user_id = #{auditUserId} AND audit_result = #{result}")
    List<AuditRecord> findByAuditUserIdAndResult(Long auditUserId, AuditResult result);

    @Select("SELECT LAST_INSERT_ID()")
    Long getLastInsertId();
}